package de.tum.in.eist.im.server.Registration;

import de.tum.in.eist.im.client.Registration.RegistrationService;
import de.tum.in.eist.im.shared.Security.SHAConverter;
import de.tum.in.eist.im.shared.User.User;
import de.tum.in.eist.im.server.Datastore.DatastoreManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RegistrationServiceImpl extends RemoteServiceServlet implements
		RegistrationService {

	/**
	 * try to register the user
	 * @param user the user to register
	 * @return true if registration success
	 */
	@Override
	public Boolean registerWithServer(User user) {
		// encrypt password
		try {
			user.setPassword(SHAConverter.toSHA256(user.getPassword()));
		} catch (Exception e) {
			System.out.println("Couldn't encrypt password on server!");
		}
		// try to add the user
		DatastoreManager datastore = new DatastoreManager();
		if (datastore.checkForUser(user)) {
			return false;
		} else {
			if (datastore.addUser(user))
				return true;	
			else 
				return false;
		}
		
	}
}
